"""This module contains classes and functions related to geometry."""

"""Project Euler Solutions Library

Copyright (c) 2011 by Robert Vella - robert.r.h.vella@gmail.com

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
"""

def pythagorean_triple(m, n):
    """Python implementation of Euclid's formulae for a Pythagorean triple."""
    return (int(n ** 2 - m ** 2), 2 * m * n, int(n ** 2 + m ** 2))
    
def vector_function(f):
    """Returns a modified version of the function, [f], so that an error is
    raised if the dimensionality of its input is not equal to the 
    dimensionality of its containing vector.
    """
    
    def new_function(self, v2):
        self._assert_vector_sizes(v2)
        return f(self, v2)

    return new_function

class vector:
    """This class represents a vector of arbitrary dimensions."""
    
    def __init__(self, *args):
        """Parameters:
        
            All of the parameters passed in the constructor are taken to be
            the elements of the new vector. The dimensionality for the new 
            vector is equal to the number of parameters passed.
        """
        
        self.__elements = args;
    
    def __getitem__(self, index):
        return self.__elements[index] 
    
    def __len__(self):
        return len(self.__elements)
    
    def _assert_vector_sizes(self, v2):
        """Raises an error if the dimensionality of [v2] is not equal to that
        of this vector.
        """ 
        
        if len(self) != len(v2):
            raise ValueError("Vector operations can only be attempted " + 
                        "on vectors of the same size. ")
    
    @vector_function    
    def dot(self, v2):
        """Returns the dot product of this vector and [v2]."""
        return sum(self[i] * v2[i] 
                    for i in range(len(self)))
    
    @vector_function
    def __add__(self, v2):
        return vector(*[self[i] + v2[i] 
                        for i in range(len(self))])
    
    @vector_function
    def __sub__(self, v2):
        return vector(*[self[i] - v2[i] 
                        for i in range(len(self))])


def point_in_triangle(p, a, b, c):
    """Returns true if vector [p] falls within the perimeter of the triangle
    formed by the vectors: [a], [b], [c].
    """
    
    #Python implementation of the algorithm found at this website: 
    #http://www.blackpawn.com/texts/pointinpoly/default.html 
    
    #Compute vectors.
    v0 = c - a
    v1 = b - a
    v2 = p - a
    
    #Compute dot products.
    v0dotv0 = v0.dot(v0)
    v1dotv2 = v1.dot(v2)
    v1dotv1 = v1.dot(v1)
    
    v0dotv1 = v0.dot(v1)
    v0dotv2 = v0.dot(v2)
    
    #Compute barycentric coordinates
    v = v0dotv0 * v1dotv2 - v0dotv1 * v0dotv2
    v /= v0dotv0 * v1dotv1 - v0dotv1 * v0dotv1
             
                
    if not 0 <= v <= 1:
        return False
      
    u = (v1dotv2 - v * v1dotv1) / v0dotv1
    
    #Return true if point is in the triangle.
    return 0 <= u <= 1 and u + v < 1
